package com.tx.service;

import cn.hutool.core.collection.ConcurrentHashSet;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import xxx.yyy.ws.constant.RedisKey;
import xxx.yyy.ws.manager.AssetTopicsManager;
import xxx.yyy.ws.manager.OrderTopicsManager;
import xxx.yyy.ws.utils.GzipUtil;

import java.util.Date;
import java.util.Set;

/**
 * @author dengguoxing
 * @date 2019/11/1
 * description
 **/
public class WsClient {

    // 固定参数
    private String id;

    private String uid;

    private Channel channel;

    // 更新参数
    private Set<String> topics = new ConcurrentHashSet<>(4);

    private Date connectDate = new Date();

    private Date lastMsgDate = new Date();

    // 特殊方法

    /**
     * 发送消息
     * @param msg
     */
    public void send(String msg){


    /**
     * 订阅
     * @param topic
     */
    public boolean subTopic(String topic) {
        boolean isAdd = this.topics.add(topic);
        if(isAdd) {
            // 同步到订阅管理
            if(topic.startsWith(RedisKey.ASSET)) {
              TopicsManager.subTopic(id, topic);
            }
        }

        // todo - 是否需要发送初始值
        return isAdd;
    }

    /**
     * 取消订阅
     * @param topic
     */
    public boolean unSubTopic(String topic){
        boolean isDel = this.topics.remove(topic);

            if(topic.startsWith(RedisKey.ASSET)) {
                TopicsManager.unSubTopic(id, topic);

        }
        return isDel;
    }

    /**
     * 取消全部订阅
     */
    public void unSubAllTopic(){
        for (String topic: topics) {
            this.unSubTopic(topic);
        }
    }

    /**
     * 关闭
     */
    public void close() {
        channel.close();
    }

    // get && set

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public Set<String> getTopics() {
        return topics;
    }

    public void setTopics(Set<String> topics) {
        this.topics = topics;
    }

    public Date getConnectDate() {
        return connectDate;
    }

    public void setConnectDate(Date connectDate) {
        this.connectDate = connectDate;
    }

    public Date getLastMsgDate() {
        return lastMsgDate;
    }

    public void setLastMsgDate(Date lastMsgDate) {
        this.lastMsgDate = lastMsgDate;
    }

}
